package com.example.mobile2121606109ycg051415homework;

import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class SingleTopActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tvTitle;
    private TextView tvTaskID;
    private TextView tvActivityNum;
    private Button btnStandard;
    private Button btnSingleTop;
    private Button btnSingleTask;
    private Button btnSingleInstance;
    private Button btnClose;
    private RecyclerView rvActivities;
    private static int NO = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_standard);
        initView();

        NO += 1;
        tvTitle.setText("SingleTopActivity" + NO);

        GlobalAPP.list.add(tvTitle.getText().toString());
        if (rvActivities.getAdapter() == null){
            rvActivities.setAdapter(GlobalAPP.adapter);
        }else {
            GlobalAPP.adapter.notifyDataSetChanged();
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        tvTaskID.setText("任务栈ID:" + getTaskId());
        ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
        ActivityManager.AppTask appTask = activityManager.getAppTasks().get(0);
        ActivityManager.RecentTaskInfo taskInfo = appTask.getTaskInfo();
        int num = taskInfo.numActivities;
        tvActivityNum.setText("Activity的数量:" + num);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        GlobalAPP.list.remove(tvTaskID.getText().toString());
        GlobalAPP.adapter.notifyDataSetChanged();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        GlobalAPP.adapter.notifyDataSetChanged();
    }


    private void initView() {
        tvTitle = (TextView) findViewById(R.id.tvTitle);
        tvTaskID = (TextView) findViewById(R.id.tvTaskID);
        tvActivityNum = (TextView) findViewById(R.id.tvActivityNum);
        btnStandard = (Button) findViewById(R.id.btnStandard);
        btnSingleTop = (Button) findViewById(R.id.btnSingleTop);
        btnSingleTask = (Button) findViewById(R.id.btnSingleTask);
        btnSingleInstance = (Button) findViewById(R.id.btnSingleInstance);
        btnClose = (Button) findViewById(R.id.btnClose);
        rvActivities = (RecyclerView) findViewById(R.id.rvActivities);
        btnStandard.setOnClickListener(this);
        btnSingleTop.setOnClickListener(this);
        btnSingleTask.setOnClickListener(this);
        btnSingleInstance.setOnClickListener(this);
        LinearLayoutManager layout = new LinearLayoutManager(this);
        layout.setStackFromEnd(true);
        layout.setReverseLayout(true);
        rvActivities.setLayoutManager(layout);
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent();
        switch (view.getId()) {
            case R.id.btnStandard:
                intent.setClass(this, StandardActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTop:
                intent.setClass(this, SingleTopActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleTask:
                intent.setClass(this, SingleTsakActivity.class);
                startActivity(intent);
                break;
            case R.id.btnSingleInstance:
                intent.setClass(this, SingleInstanceActivity.class);
                startActivity(intent);
                break;
        }
    }
}